package com.cooper.mediadb.online;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.cooper.scraper.DownloadException;
import com.cooper.scraper.HttpDownload;
import com.cooper.scraper.ScraperException;

public class AnalyzeTraileraddict extends AnalyzeVideo {

	private String id = null;

	public AnalyzeTraileraddict(HttpDownload httpDownload, String url,
			String format) {
		super(httpDownload, url, format);
	}

	public AnalyzeTraileraddict(String id, HttpDownload httpDownload,
			String format) {
		super(httpDownload, format);
		this.id = id;
	}

	@Override
	public List<String> analyzeVideo() throws DownloadException,
			ScraperException, IOException {
		List<String> result = new ArrayList<String>();
		if ((id != null) && (!"".equals(id.trim()))) {
			String page = httpDownload
					.downloadPage("http://www.traileraddict.com/fvare.php?tid="
							+ id.trim());
			Matcher m = Pattern.compile(
					"&fileurl=(http://.+?\\.traileraddict\\.com/[^%]+)%3F([^&]*)&")
					.matcher(page);
			if (m.find()){
				result.add(m.group(1) + "?" + m.group(2));
			}else {
				m = Pattern.compile(
						"&fileurl=(http://.+?\\.traileraddict\\.com/[^%]+)%")
						.matcher(page);
				if (m.find()){
					result.add(m.group(1));
				}
			}

		}
		return result;
	}

}
